package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/10 15:25
 * @Version 1.0.0
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {
        //创建一个套餐对象，用于传递参数
        Setmeal setmeal = new Setmeal();
        //将前端传递进来的参数拷贝到setmeal对象中
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //将套餐进行保存
        setmealMapper.saveSetmeal(setmeal);
        //获取套餐下面的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //将菜品和套餐进行关联
            setmealDish.setSetmealId(setmeal.getId());
            //将菜品和套餐的关系进行保存
            setmealMapper.saveSetmealDish(setmealDish);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //创建一个返回对象
        PageResult pageResult = new PageResult();
        List<SetmealVO> list = new ArrayList<SetmealVO>();
        //设置起始页数和每行显示条数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //进行查询
        Page<Setmeal> setmeals = setmealMapper.pageQuery(setmealPageQueryDTO);
        //结果进行赋值
        pageResult.setTotal(setmeals.getTotal());
        List<Setmeal> result = setmeals.getResult();
        for (Setmeal setmeal : result) {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal,setmealVO);
            setmealVO.setCategoryName(categoryMapper.selectById(setmeal.getCategoryId()).getName());
            list.add(setmealVO);
        }
        pageResult.setRecords(list);
        return pageResult;
    }

    //TODO :后续看是否要修改
    @Override
    public Result deleteSetmeal(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectSetmealById(id);
            if (setmeal.getStatus()==1){
                return Result.error(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //将套餐进行删除
        setmealMapper.deleteSetmeal(ids);
        //将套餐中的菜品进行删除
        setmealMapper.deleteSetmealDish(ids);
        return Result.success();
    }

    /**
     * 根据id获取套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectSetmealById(Long id) {
        //创建一个返回类型
        SetmealVO setmealVO = new SetmealVO();
        //获取套餐信息
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        //获取套餐下的菜品信息
        List<SetmealDish> setmealDishes = setmealMapper.selectSetmealDishes(id);
        //将相同属性进行拷贝
        BeanUtils.copyProperties(setmeal,setmealVO);
        //将菜品信息进行赋值
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @Override
    public void updateSetmealById(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //首先将套餐里面的信息进行修改
        setmealMapper.updateSetmealById(setmeal);
        //然后将改套餐的菜品进行删除
        Long[] arr = new Long[1];
        arr[0] = setmealDTO.getId();
        setmealMapper.deleteSetmealDish(arr);
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealMapper.saveSetmealDish(setmealDish);
        }
    }

    @Override
    public Result updateStatusById(Integer status, Long id) {
        //查询当前套餐下面的菜品
        List<SetmealDish> setmealDishes = setmealMapper.selectSetmealDishes(id);
        //查看是否有菜品是未启售状态
        for (SetmealDish setmealDish : setmealDishes) {
            //查询单个菜品
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            //进行判断，要是状态为0直接启售失败
            if (StatusConstant.DISABLE.equals(dish.getStatus())){
                return Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        //创建一个用于修改的对象
        Setmeal setmeal = new Setmeal();
        //设置状态和要修改的套餐id
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmealStatusById(setmeal);
        return Result.success();
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
